//
//  LottieAnimationProtocol.swift
//  IAM
//
//  Created by Rae on 2023/1/9.
//

import UIKit

import Lottie

struct RaeLottieAnimationWrapper<Base> {
    let base: Base
    init(_ base: Base) {
        self.base = base
    }
}

protocol RaeLottieAnimationProtocol { }
extension RaeLottieAnimationProtocol {
    ///
    public var animation: RaeLottieAnimationWrapper<Self> {
        get { return RaeLottieAnimationWrapper(self) }
        set { }
    }
    
}

typealias RaeLottieAnimationView = LottieAnimationView

extension RaeLottieAnimationWrapper {
    
    func make(_ json: RaeJsonType, loopMode: LottieLoopMode = .loop, speed: CGFloat = 1.0) -> RaeLottieAnimationView {
        make(json, loopMode: loopMode, speed: speed, playFinishBlock: nil)
    }
    
    func make(_ name: RaeJsonType, loopMode: LottieLoopMode = .loop, speed: CGFloat = 1.0, contentMode: UIView.ContentMode = .scaleAspectFill, playFinishBlock: ((Bool) -> Void)? = nil) -> RaeLottieAnimationView {
        let view = RaeLottieAnimationView(name: name.rawValue, bundle: .main)
        view.contentMode = contentMode
        view.loopMode = loopMode
        view.shouldRasterizeWhenIdle = true
        view.backgroundBehavior = .pauseAndRestore
        view.currentProgress = 0
        view.animationSpeed = speed
        view.play(completion: playFinishBlock)
        return view
    }
    
}

extension RaeLottieAnimationWrapper {
    
    enum RaeJsonType: String {
        
        //loading
        case loading = "loading"
        case bellAnimation = "bellAnimation"
    }
    
}

